using System;
using System.Web;
using System.Web.Security;
using PetShop.Domain.Model;
using PetShop.Application.Facade;
using System.Collections.Generic;


namespace PetShop.Web.ProcessFlow{

	
	
	
	public class CartController{

	 
		private const string BILLING_KEY = "BILLING_KEY";
		private const string SHIPPING_KEY = "SHIPPING_KEY";
		private const string ACCOUNT_KEY = "ACCOUNT_KEY";
		private const string CART_KEY = "CART_KEY";
		private const string CREDITCARD_KEY = "CREDITCARD_KEY";
		private const string URL_NOCART = "ShoppingCart.aspx";

		public CartController(){}

		
		public CartInfo GetCartInfo(bool create){
			CartInfo cart;

			cart = (CartInfo)HttpContext.Current.Session[CART_KEY];

			if ( cart == null ){
				if (create){
					cart = new CartInfo();
				}else{			
					HttpContext.Current.Server.Transfer(URL_NOCART);
					return null;
				}
			}
			
			return cart;
		}


   

		public void AddItemToCart(CartInfo cart, string ItemId)
		{
			OrderFC cFcd = new OrderFC();


			cart = cFcd.AddItemToCart(cart, ItemId);

		 
		   
		  
		  
		}
		
		
		public void RememberCart(CartInfo cart){
		
			
			HttpContext.Current.Session[CART_KEY] = cart;

		}





		private static Account GetUserInfo()
		{
			return ((Account)HttpContext.Current.Session[ACCOUNT_KEY]);
		}

		private static CreditCardInfo GetCreditCardInfo()
		{
			return (CreditCardInfo)HttpContext.Current.Session[CREDITCARD_KEY];
		}




		private static object EmptyShippingAddressInfo()
		{
			return HttpContext.Current.Session[SHIPPING_KEY] = null;
		}

		private static object EmptyBillingAddressInfo()
		{
			return HttpContext.Current.Session[BILLING_KEY] = null;
		}

		private static object EmptyCreditCardInfo()
		{
			return HttpContext.Current.Session[CREDITCARD_KEY] = null;
		}

		private static object EmptyCartInfo()
		{
			return HttpContext.Current.Session[CART_KEY] = null;
		}

		public Order PurchaseCart()
		{

			CartInfo cart;
			cart = (CartInfo)HttpContext.Current.Session[CART_KEY];


			if ((cart == null) || (cart.Count == 0))
			{

				HttpContext.Current.Server.Transfer(URL_NOCART);

				return null;

			}
			else
			{


				Account userInfo = GetUserInfo();
				CreditCardInfo creditCardInfo = GetCreditCardInfo();
				AddressInfo billingAddressInfo = this.GetBillingAddressInfo();
				AddressInfo shippingAddressInfo = this.GetShippingAddressInfo();

				OrderFC fcd = new OrderFC();


				Order newOrder = fcd.PrepareOrder(userInfo,
						creditCardInfo,
						billingAddressInfo,
						shippingAddressInfo,
						cart);


				fcd.RegisterOrder(newOrder);


				EmptyCartInfo();
				EmptyCreditCardInfo();
				EmptyBillingAddressInfo();
				EmptyShippingAddressInfo();

				return newOrder;
			}
		}



		
		public void StoreCreditCard(CreditCardInfo creditCard){

			HttpContext.Current.Session[CREDITCARD_KEY] = creditCard;
				
		}

		
		
		
		public void StoreBillingAddress(AddressInfo billingAddress){

			HttpContext.Current.Session[BILLING_KEY] = billingAddress;
				
		}

		
		
		
		public void StoreShippingAddress(AddressInfo shippingAddress){

			HttpContext.Current.Session[SHIPPING_KEY] = shippingAddress;
				
		}

		
		
		
		public AddressInfo GetBillingAddressInfo(){

			return (AddressInfo)HttpContext.Current.Session[BILLING_KEY];
				
		}

		
		
		
		public AddressInfo GetShippingAddressInfo(){

			return (AddressInfo)HttpContext.Current.Session[SHIPPING_KEY];
				
		}

		
		
		
		
		public void ContinueOrder(bool useBillingAddress){

			
			if (useBillingAddress) {
				HttpContext.Current.Session[SHIPPING_KEY] = HttpContext.Current.Session[BILLING_KEY];
			} else {
				
				HttpContext.Current.Response.Redirect("OrderShipping.aspx", true);
			}				
		}

		
		
		
		public void SetAlternateShippingAddress(AddressInfo shippingAddress){

			HttpContext.Current.Session[SHIPPING_KEY] = shippingAddress;
		}
	}
}
